<?php

namespace App\Http\Controllers\Admin;

use App\Models\SensitiveWord;
use App\Validate\SensitiveWordValidate;
use Illuminate\Support\Facades\DB;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;

/**
 * 敏感词
 */
class SensitiveWordController extends CommonController
{
    public $model = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new SensitiveWord();
        $this->validate = new SensitiveWordValidate();
    }

    /**
     * 敏感词(用于下拉框选择)
     */
    public function filterList()
    {
        return $this->model->getFilterList(['id', 'name']);
    }

    /**
     * 获取数字资源大类型
     * @param keywords 检索条件
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function lists()
    {
        $limit = $this->request->input('limit', 10);
        $page = $this->request->input('page', 1);
        $res = $this->model->lists($this->request->keywords, $limit);
        if (empty($res['data'])) {
            return $this->returnApi(203, '暂无数据');
        }
        //增加序号
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);
        $res = $this->disPageData($res);
        return $this->returnApi(200, '获取成功', true, $res);
    }

    /**
     * 详情
     * @param id int 类型id
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->select('id', 'name', 'create_time')->find($this->request->id);

        if (!$res) {
            return $this->returnApi(201, "参数传递错误");
        }

        return $this->returnApi(200, "获取成功", true, $res->toArray());
    }

    /**
     * 新增
     * @param name string 敏感词名称 多个用 | 拼接
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        DB::beginTransaction();
        try {
            $data = $this->request->all();
            $this->model->add($data);
            DB::commit();
            return $this->returnApi(200, "新增成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, "新增失败");
        }
    }

    /**
     * 修改
     * @param id int 类型id
     * @param name string 敏感词名称
     */
    public function change()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        if (strpos($this->request->name, "|") != false) {
            return $this->returnApi(202, "敏感词不能包含 | 符号");
        }
        $is_exists = $this->model->where('name', $this->request->name)->first();
        if ($is_exists) {
            return $this->returnApi(202, "该敏感词已存在");
        }
        $res = $this->model->change($this->request->all());
        if (!$res) {
            return $this->returnApi(202, "修改失败");
        }
        return $this->returnApi(200, "修改成功", true);
    }

    /**
     * 删除
     * @param id int 类型id
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $res = $this->model->where('id', $this->request->id)->delete();
        if ($res) {
            return $this->returnApi(200, "删除成功", true);
        }
        return $this->returnApi(202, $res);
    }

    /**
     * 根据敏感词重置数据
     */
    public function resetSensitiveWordData()
    {
        //重置一次数据
        if (Cache::get('ResetResourceNumberAndMaskData')) {
            return $this->returnApi(201, "5分钟之内只能重置一次，请稍后重试");
        }
        $command = 'command:ResetResourceNumberAndMaskData';
        $artisan = App::make(Kernel::class);
        $artisan->call($command, [
            // '--option' => 'value',
        ]);
        Cache::put('ResetResourceNumberAndMaskData', 1, 60 * 5); //5分钟后重置一次数据
        return $this->returnApi(200, "重置成功", true);
    }
}
